<?php

class ExportRSS
{
	public $data = array();
	public $channel = array();
	public $rawdata = array();

	public function __construct($data, $version)
	{

            $this->data = array();
        
            //$feed = @file_get_contents($data);
            //if (!$feed) {
            //    return false;
            //}
        
		$this->rawdata = @simplexml_load_string($data);
		//print_r($this->rawdata);
        if (!$this->rawdata) {
            return false;
        }
        
		// Parse the XML/RSS file
		switch ($version)
		{
			case "1.0":
			{
				$this->channel = array(
					"title"       => (string)$this->rawdata->channel->title,
					"link"        => (string)$this->rawdata->channel->link,
					"description" => (string)$this->rawdata->channel->description,
					"image"       => (string)$this->rawdata->channel->image,
					"items"       => (string)$this->rawdata->channel->items,
					"textinput"   => (string)$this->rawdata->channel->textinput
				);
				
				foreach ($this->rawdata->item as $item)
				{
                    
					$row = array(
						"title"       => (string)$item->title,
						"link"        => (string)$item->link,
						"description" => (string)$item->description
					);
					array_push($this->data, $row);
				}
                
				break;
			}
			case "2.0":
			{
				$this->channel = array(
					"title"       => $this->rawdata->channel->title,
					"link"        => $this->rawdata->channel->link,
					"description" => $this->rawdata->channel->description,
					"language"    => $this->rawdata->channel->language,
					"date"        => $this->rawdata->channel->pubDate,
					"builddate"   => $this->rawdata->channel->lastBuildDate,
					"docs"        => $this->rawdata->channel->docs,
					"generator"   => $this->rawdata->channel->generator,
					"editor"      => $this->rawdata->channel->managingEditor,
					"webmaster"   => $this->rawdata->channel->webMaster,
                    "image"       => $this->rawdata->channel->image,
				);
				
				foreach ($this->rawdata->channel->item as $item)
				{
					$row = array(
						"title"       => (string)$item->title,
						"link"        => (string)$item->link,
						"description" => strip_tags((string)$item->description),
						//"enclosure"   => $this->enclosure,
						"date"        => (string)$item->pubDate,
						"guid"        => (string)$item->guid,
                        "image"       => (string)$item->image
					);
					array_push($this->data, $row);
				}
                
				break;
                
			}
			default:
			{
				//echo "ExportRSS::WARNING: invalid version was specified";
				break;
			}
		}
	}
	
	public function get_raw_data()
	{
		return $this->rawdata;
	}
	
	public function get_channel_data()
	{
		return $this->channel;
	}
	
	public function get_data()
	{
		return $this->data;
	}
}